package com.neusoft.weekreport.component.controller;

import com.neusoft.weekreport.component.bizService.MenuBizService;
import com.neusoft.weekreport.component.req.GetAddMenuInfoReq;
import com.neusoft.weekreport.component.req.GetMenuByUserIdReq;
import com.neusoft.weekreport.component.req.GetMenuInfoByMenuIdReq;
import com.neusoft.weekreport.component.resp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping("/menu")

public class MenuController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    MenuBizService menuBizService;

    //根据用户id获取菜单
    @RequestMapping(value = "/getMenuByUserId", method = RequestMethod.GET)

    public GetMenuByUserIdListResp login(GetMenuByUserIdReq req) {
        GetMenuByUserIdListResp resp = new GetMenuByUserIdListResp();
        try {
            resp = menuBizService.getBgMenu(req);
        } catch (Exception e) {
            resp.setMsg("导航栏菜单查询失败！");
        }
        return resp;
    }

    //查询所有菜单树
    @RequestMapping(value = "/menuManagement", method = RequestMethod.GET)

    public ResponseBean<GetMenuInfoListResp> getMenuInfoList(GetMenuInfoByMenuIdReq req, HttpServletRequest request) {
        ResponseBean<GetMenuInfoListResp> responseBean = new ResponseBean<>();
        try {
            GetMenuInfoListResp resp = menuBizService.getMenuInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询菜单树成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //通过菜单id查询菜单信息
    @RequestMapping(value = "/getMenuInfoByMenuId",method = RequestMethod.GET)

    public ResponseBean<GetMenuInfoByMenuIdResp> getMenuInfoByMenuId(GetMenuInfoByMenuIdReq req,HttpServletRequest request){
        ResponseBean<GetMenuInfoByMenuIdResp> responseBean = new ResponseBean<>();
        try{
            GetMenuInfoByMenuIdResp resp = menuBizService.getMenuInfoByMenuId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询菜单信息成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //添加新的菜单
    @RequestMapping(value = "/menuManagement",method = RequestMethod.POST)
    public ResponseBean<GetAddMenuInfoResp> addMenuInfo(@RequestBody GetAddMenuInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddMenuInfoResp> responseBean = new ResponseBean();
        try{
            GetAddMenuInfoResp resp = menuBizService.addMenuInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"添加菜单成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //修改菜单信息
    @RequestMapping(value = "menuManagement",method = RequestMethod.PUT)
    public  ResponseBean modifyMenuInfoByMenuId(@RequestBody GetAddMenuInfoReq req,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try{
            menuBizService.modifyMenuInfoByMenuId(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"修改菜单成功",responseBean);
        }catch(Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //删除菜单信息
    @RequestMapping(value = "menuManagement",method = RequestMethod.DELETE)
    public ResponseBean<GetCommenResp> deleteMenuInByMenuId(@RequestBody GetMenuInfoByMenuIdReq req,HttpServletRequest request){
        ResponseBean<GetCommenResp> responseBean = new ResponseBean<>();
        try{
            GetCommenResp resp = menuBizService.deleteMenuInByMenuId(req);
            responseBean = new ResponseBean<>(HttpStatus.OK.value(),"删除菜单失败",resp);

        }catch(Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }
}
